

package com.hexinfo.dmpro.script.pack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.script.config.model.ConfigPathGit;
import com.hexinfo.dmpro.script.pack.entity.ScriptPackTaskEntity;
import com.hexinfo.dmpro.script.pack.service.IScriptPackTaskService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 脚本打包任务表
 *
 * @author WZH
 * @date 2023-09-13 16:13:23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/api/scriptPack/task")
public class ScriptPackTaskController {

    private final IScriptPackTaskService scriptPackTaskService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param scriptPackTask 脚本打包任务表
     *                       WrapperEnhancer.like(scriptPackTask); //自动拼接存在值sql的方法
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public ResponseEntity<Object> getScriptPackTaskPage(Page page, ScriptPackTaskEntity scriptPackTask) {
        return new ResponseEntity<>(scriptPackTaskService.queryList(page, scriptPackTask), HttpStatus.OK);
    }


    /**
     * 通过id查询脚本打包任务表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public ResponseEntity<Object> getById(@PathVariable("id") String id) {
        return ResponseEntity.ok(scriptPackTaskService.getById(id));
    }

    /**
     * 新增脚本打包任务表
     *
     * @param scriptPackTask 脚本打包任务表
     * @return R
     */
    @ApiOperation(value = "新增脚本打包任务表", notes = "新增脚本打包任务表")
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody ScriptPackTaskEntity scriptPackTask) {
        return ResponseEntity.ok(scriptPackTaskService.createPackTask(scriptPackTask));
    }

    /**
     * 修改脚本打包任务表
     *
     * @param scriptPackTask 脚本打包任务表
     * @return R
     */
    @ApiOperation(value = "修改脚本打包任务表", notes = "修改脚本打包任务表")
    @PutMapping
    public ResponseEntity<Object> updateById(@RequestBody ScriptPackTaskEntity scriptPackTask) {
        return ResponseEntity.ok(scriptPackTaskService.updateById(scriptPackTask));
    }

    /**
     * 通过id删除脚本打包任务表
     *
     * @param ids
     * @return R
     */
    @ApiOperation(value = "通过id删除脚本打包任务表", notes = "通过id删除脚本打包任务表")
    @DeleteMapping(value = "/{ids}")
    public ResponseEntity<Object> removeById(@PathVariable("ids") @NotNull List<String> ids) {
        scriptPackTaskService.removeByIds(ids);
        return ResponseEntity.ok("删除成功");
    }


    /**
     * @param file
     * @Method upload
     * @Param
     * @Return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description 扫描脚本上传
     * @Author Wang zhihao
     * @Date 2023/9/14 20:26
     * @Version V1.0
     */
    @ApiOperation(value = "扫描脚本上传", notes = "扫描脚本上传")
    @PostMapping(value = "/upload", produces = "application/json;charset=utf-8")
    public ResponseEntity<Object> upload(@RequestParam("file") MultipartFile file) {
        return ResponseEntity.ok(scriptPackTaskService.upload(file));
    }

    /**
     * 打包脚本文件
     *
     * @param scriptPackTask
     * @return R
     */
    @ApiOperation(value = "打包脚本文件", notes = "打包脚本文件")
    @PostMapping("/pack")
    public ResponseEntity<Object> packFile(@RequestBody ScriptPackTaskEntity scriptPackTask) {
        return ResponseEntity.ok(scriptPackTaskService.packFile(scriptPackTask));
    }

    //通过git路径，用户名，密码获取所有分支集合
    @GetMapping("/branch")
    @AnonymousAccess
    public List<String> getBranch(@RequestParam String gitpath,@RequestParam String username,@RequestParam String password ) throws Exception {
        Collection<Ref> branches = Git.lsRemoteRepository()
                .setRemote(gitpath)
                .setHeads(true)
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
                .call();
        // 获取分支全名
        List<String> branchFullNames = branches.stream().map(Ref::getName).filter(name -> name.startsWith("refs/heads/")).collect(Collectors.toList());
        List<String> branchNames = branchFullNames.stream().map(s -> s.split("/")[s.split("/").length - 1]).collect(Collectors.toList());
        return branchNames;
    }




}
